C# Excel by Merging Excel Files into One
If you face to tons of Excel files you may want to merge them together so that you don’t have to open them one by one. Merge Excel files together into one can make Excel developers’ work much more convenient. Spire.XLS, an easy-to-use and powerful .NET/Silverlight Excel component allows Excel developers merge multiple Excel files into one with C#/VB.NET. This article will introduce how to use C# to merge multiple Excel files into one via Spire.XLS.
Download Spire.XLS, make sure Spire.XLS and Visual Studio are correctly installed on system. Follow the guide below to merge Excel files.
Download Spire.XLS, make sure Spire.XLS and Visual Studio are correctly installed on system. Follow the guide below to merge Excel files.
C# Merge Excel Files via Spire.XLS
Step 1 Create Project
Create a C#/VB.NET project in your visual studio. Add Spire.XLS.dll as reference. The default setting of Spire.Xls.dll is placed under "C:\Program Files\e-iceblue\Spire.Xls\Bin”. Select assembly Spire.Xls.dll and click OK to add it to the project.
using Spire.Xls;
namespace FilesMerge
{
class Program
{
static void Main(string[] args)
{
}
}
}
Create a C#/VB.NET project in your visual studio. Add Spire.XLS.dll as reference. The default setting of Spire.Xls.dll is placed under "C:\Program Files\e-iceblue\Spire.Xls\Bin”. Select assembly Spire.Xls.dll and click OK to add it to the project.
using Spire.Xls;
namespace FilesMerge
{
class Program
{
static void Main(string[] args)
{
}
}
}
Step 2 Load Excel Files
Put the Excel files which need be merged together into the project folder. Use the code below to load the Excel files into the project.
//load the first workbook
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\parts.xls", ExcelVersion.Version97to2003);
//load the second workbook
Workbook workbook2 = new Workbook();workbook2.LoadFromFile(@"..\..\country.xls", ExcelVersion.Version97to2003);
Put the Excel files which need be merged together into the project folder. Use the code below to load the Excel files into the project.
//load the first workbook
Workbook workbook = new Workbook();
workbook.LoadFromFile(@"..\..\parts.xls", ExcelVersion.Version97to2003);
//load the second workbook
Workbook workbook2 = new Workbook();workbook2.LoadFromFile(@"..\..\country.xls", ExcelVersion.Version97to2003);
Step 3 Merge Excel Files
Insert the content of Excel files into the first Excel file by using the following code.
//insert the second workbook's worksheet into the first workbook using a dataTable
Worksheet sheet2 = workbook2.Worksheets[0];
DataTable dataTable = sheet2.ExportDataTable();
Worksheet sheetAdd = workbook.CreateEmptySheet("Country");sheetAdd.InsertDataTable(dataTable, true, 1, 1);
Insert the content of Excel files into the first Excel file by using the following code.
//insert the second workbook's worksheet into the first workbook using a dataTable
Worksheet sheet2 = workbook2.Worksheets[0];
DataTable dataTable = sheet2.ExportDataTable();
Worksheet sheetAdd = workbook.CreateEmptySheet("Country");sheetAdd.InsertDataTable(dataTable, true, 1, 1);
Step 4 Save and Preview
After finish merging Excel files, save the modified Excel file as .xls format. And we can preview what we’ve done by pressing F5 to launch the project.
//save the workbook
workbook.SaveToFile(@"..\..\FilesMerge.xls", ExcelVersion.Version97to2003);
//launch the workbook
System.Diagnostics.Process.Start(@"..\..\FilesMerge.xls");
After finish merging Excel files, save the modified Excel file as .xls format. And we can preview what we’ve done by pressing F5 to launch the project.
//save the workbook
workbook.SaveToFile(@"..\..\FilesMerge.xls", ExcelVersion.Version97to2003);
//launch the workbook
System.Diagnostics.Process.Start(@"..\..\FilesMerge.xls");
Spire.XLS is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET and Silverlight. It supports C#, VB.NET, ASP.NET, ASP.NET MVC and Silverlight. Click to learn more...